<template>
  <el-row class="top-header">
    <el-col :span="24">
      <div class="top-header__logo" @click="goHome">
        <img src="@/assets/logo.png">
      </div>
      <div class="top-header__title" v-show="showTitle">
        <span @click="goHome">新德汇前端基础框架 v {{version}}</span>
      </div>
      <div class="top-header__nav pull-right" v-if="nav" :class="{'top-header__nav--expand':showTitle}">
        <xdh-menu :data="nav"
                  :default-active="menuActive"
                  mode="horizontal"
                  :router="false"
                  @select="handleSelect"></xdh-menu>
      </div>
    </el-col>
    <!--  <el-col :span="1">
        <div class="top-header__tools">
          <search-input></search-input>
          <div class="tool-item">
          <el-dropdown>
          <div>
          <xdh-avatar :src="head">User</xdh-avatar>
          管理员
          </div>
          <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>我的消息
          <el-badge :value="123"></el-badge>
          </el-dropdown-item>
          <el-dropdown-item>设置</el-dropdown-item>
          <el-dropdown-item divided>退出</el-dropdown-item>
          </el-dropdown-menu>
          </el-dropdown>
          </div>
          <div class="tool-item" @click.stop="handleSystem">
          <i class="el-icon-setting"></i>
          </div>
        </div>

      </el-col>-->
  </el-row>
</template>

<script>
  import XdhMenu from '@/widgets/xdh-menu'
  import XdhAvatar from '@/widgets/xdh-avatar'
  import SearchInput from './search-input.vue'

  const packageJSON = require('../../package.json')
  const DEFAULT_HEAD = require('@/assets/images/head.jpg')
  const navData = [{
    id: '/guide/changelog',
    text: '开发指南'
  }, {
    id: '/widgets',
    text: '示例'
  }, {
    id: '/src/%2Fstatic%2Fwidgets%2Findex.html',
    text: 'Widgets'
  }, {
    id: '/src/%2Fstatic%2Futils%2Findex.html',
    text: 'Utils'
  }, {
    id: '/libs',
    text: '第三方类库'
  }, {
    id: '/src/%2Fstatic%2Fproject%2Findex.html',
    text: '项目'
  }, {
    id: '/src/%2Fstatic%2Fschemas%2Findex.html',
    text: 'Schemas'
  }]
  export default {
    components: {
      XdhMenu,
      XdhAvatar,
      SearchInput
    },
    props: {
      showTitle: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        head: DEFAULT_HEAD,
        nav: navData,
        version: packageJSON.version,
        menuActive: null
      }
    },
    watch: {
      '$route.path'(path) {
        this.setMenuActive(path)
      }
    },
    methods: {
      /**
       * 进去系统管理
       */
      handleSystem() {
        this.$router.push('/admin')
      },
      goHome() {
        this.$router.push('/')
      },
      handleSelect(path) {
        if (path === '/static') {
          location.href = '/static/index.html'
        } else {
          this.menuActive = path;
          this.$router.push(path)
        }
      },
      setMenuActive(path) {
        this.nav.forEach(item => {
          if (path.includes(item.id)) {
            this.menuActive = item.id
          }
        })
      }
    },
    created() {
      this.setMenuActive(this.$route.path)
    }
  }
</script>
